<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "customer".
 *
 * @property integer $cust_id
 * @property string $cust_name
 * @property string $address
 * @property string $person_in_charge
 * @property string $email
 * @property string $tel
 * @property string $ns_person
 * @property integer $close_fee
 * @property integer $close_month
 * @property string $cust_type
 * @property boolean $is_operating
 * @property integer $close_compensation_month
 * @property integer $payroll_compensation
 * @property integer $payroll_month
 * @property integer $payroll_compensation_month
 * @property boolean $is_tax_form
 * @property boolean $is_bookkeeping
 * @property boolean $is_labor_form
 * @property boolean $is_year_end_adjustment
 * @property boolean $is_legal_adjustment
 * @property string $declaration_form
 * @property string $notification_form
 * @property string $remark
 * @property string $advisory_fee
 * @property string $advisory_from
 * @property string $advisory_to
 * @property FeeItem[] $feeItems
 * @property integer $other_fee_amount
 */
class Customer extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['close_fee', 'close_month', 'close_compensation_month', 'payroll_compensation', 'payroll_month', 'payroll_compensation_month', 'advisory_fee'], 'integer'],
            [['advisory_from_month', 'advisory_from_year', 'advisory_to_month', 'advisory_to_year'], 'integer'],
            [['is_operating', 'is_tax_form', 'is_bookkeeping', 'is_labor_form', 'is_year_end_adjustment', 'is_legal_adjustment'], 'boolean'],
            [['cust_name', 'address', 'person_in_charge', 'tel', 'ns_person'], 'required'],
            [['email'], 'email'],
            [['cust_name', 'cust_type', 'tel', 'ns_person'], 'string', 'max' => 50],
            [['address', 'declaration_form', 'notification_form', 'remark'], 'string', 'max' => 255],
            [['person_in_charge', 'email'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cust_id' => Yii::t('app', 'Customer ID'),
            'cust_name' => Yii::t('app', 'Customer name'),
            'address' => Yii::t('app', 'Address'),
            'person_in_charge' => Yii::t('app', 'CL person'),
            'email' => Yii::t('app', 'Email'),
            'tel' => Yii::t('app', 'Tel'),
            'ns_person' => Yii::t('app', 'Ns person'),
            'close_fee' => Yii::t('app', 'Close fee'),
            'close_month' => Yii::t('app', 'Close month'),
            'cust_type' => Yii::t('app', 'Cust Type'),
            'is_operating' => Yii::t('app', 'Is operating'),
            'close_compensation_month' => Yii::t('app', 'Close compensation month'),
            'payroll_compensation' => Yii::t('app', 'Payroll compensation'),
            'payroll_month' => Yii::t('app', 'Payroll month'),
            'payroll_compensation_month' => Yii::t('app', 'Payroll compensation month'),
            'is_tax_form' => Yii::t('app', 'Is tax form'),
            'is_bookkeeping' => Yii::t('app', 'Is bookkeeping'),
            'is_labor_form' => Yii::t('app', 'Is labor form'),
            'is_year_end_adjustment' => Yii::t('app', 'Is year end adjustment'),
            'is_legal_adjustment' => Yii::t('app', 'Is legal adjustment'),
            'declaration_form' => Yii::t('app', 'Declaration form'),
            'notification_form' => Yii::t('app', 'Notification form'),
            'remark' => Yii::t('app', 'Remark'),
            'advisory_fee' => Yii::t('app', 'Advisory fee'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFeeItems()
    {
    	return $this->hasMany(FeeItem::className(), ['cust_id' => 'cust_id']);
    }

    public static function getListNSPerson(){
    	$query = new \yii\db\Query();
    	$query -> select('ns_person') -> distinct() -> from('customer');

    	$list = $query -> all();

    	return $list;
    }

    public static function getLastID($is_operating){
        $query = new \yii\db\Query();
        $query -> select('cust_id') -> from('customer');
        $query -> orderBy(['cust_id' => SORT_DESC]);
        $query -> where(['is_operating' => $is_operating]);
        $obj = $query -> one();

        if($is_operating == true){
            if(!$obj){
                $last_id = 900000;
            }else{
                $last_id = $obj['cust_id'];
            }
        }
        else
        {
            if(!$obj){
                $last_id = 100000;
            }else{
                $last_id = $obj['cust_id'];
            }
        }

        return $last_id + 1;
    }
}
